package ghostcat.util
{
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import ghostcat.events.*;
    import ghostcat.util.core.*;

    public class Tick extends Singleton
    {
        private var prevTime:int;
        private var displayObject:Sprite;
        public var pause:Boolean = false;
        public var speed:Number = 1;
        public static var MAX_INTERVAL:int = 300;
        public static var frameRate:Number = NaN;

        public function Tick()
        {
            displayObject = new Sprite();
            displayObject.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            return;
        }// end function

        private function enterFrameHandler(event:Event) : void
        {
            var _loc_3:int = 0;
            var _loc_4:TickEvent = null;
            var _loc_2:* = getTimer();
            if (!pause)
            {
                if (prevTime == 0)
                {
                    _loc_3 = 0;
                }
                else
                {
                    _loc_3 = Math.min(_loc_2 - prevTime, MAX_INTERVAL);
                    _loc_4 = new TickEvent(TickEvent.TICK);
                    _loc_4.interval = _loc_3 * speed;
                    dispatchEvent(_loc_4);
                }
            }
            prevTime = _loc_2;
            return;
        }// end function

        public function clear() : void
        {
            this.prevTime = 0;
            return;
        }// end function

        public static function get instance() : Tick
        {
            return Singleton.getInstanceOrCreate(Tick) as Tick;
        }// end function

    }
}
